//
//  SLMainController.swift
//  Sina15
//
//  Created by 盼 on 16/4/6.
//  Copyright © 2016年 pan. All rights reserved.
//

import UIKit

class SLMainController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //把系统tabBar替换成自定义的tabBar
        let myTabBar = SLTabBar()

//      print(tabBar.classForCoder)
        setValue(myTabBar, forKey: "tabBar")
    //  print(tabBar.classForCoder)
     
        addChildViewControllers()
        
        
        //给加号按钮添加点击事件
       myTabBar.plusBtn.addTarget(self, action: "plusBtnDidClick", forControlEvents: .TouchUpInside)
        
    }
    
    //点击“＋”按钮显示模糊效果
  @objc private func plusBtnDidClick(){
        
        let composeView = SLComposeView()
    
        composeView.show(self)
    
//        let window = UIApplication.sharedApplication().keyWindow
//    
//        window?.addSubview(composeView)
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //设置tabBar控制器的根控制器
   private func addChildViewControllers() {
        
        addChildViewController(SLHomeController(), title: "首页", imageName: "tabbar_home",index: 0)
    
        addChildViewController(SLMessageController(), title: "消息", imageName: "tabbar_message_center",index: 1)
        
        addChildViewController(SLDiscoveryController(), title: "发现", imageName: "tabbar_discover",index: 2)
 
        addChildViewController(SLProfileController(), title: "我", imageName: "tabbar_profile",index: 3)
        
    }
    
    private let baseTag = 1000
    //重载添加子控制器的方法
    private func addChildViewController(childVC: UIViewController,title: String,imageName: String,index: Int) {
        
        //使用自定义的tabBarItem替换当前系统的tabBarItem
        childVC.tabBarItem = SLTabBarItem()
        
        childVC.tabBarItem.tag = baseTag + index
        childVC.title = title
        childVC.tabBarItem.image = UIImage(named: imageName)
    
    //设置选中图片用原本的渲染方式
        childVC.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
    
    //设置tabBar item的title的属性
    childVC.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: UIControlState.Selected)

        //创建导航控制器
        let nav = SLBaseNavController(rootViewController: childVC)
        
        //导航控制器作为tabBar控制器的子控制器
        addChildViewController(nav)
    }
    
    //MARK: 被点击按钮实现动画效果
    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        print(item.tag)
        
        var index = 0
        for subview in tabBar.subviews {
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                if index == item.tag - baseTag {
                    
                    for sub in subview.subviews {
                        if sub.isKindOfClass(NSClassFromString("UITabBarSwappableImageView")!) {
                            
                            sub.transform = CGAffineTransformMakeScale(0.5, 0.5)
                            UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.9, options: [], animations: { () -> Void in
                                
                                sub.transform = CGAffineTransformMakeScale(1, 1)
                                }, completion: { (_) -> Void in
                                    print("OK")
                            })
                        }
                    }
                }
                index++
            }
        }
    }

}
